package org.paic.vertx.utils.db;

public class Dialect {
	public static final String RS_COLUMN = "NUMS";

	public static String getPagiSql(String sql, int offset, int limit, boolean limitSupport) {
		sql = sql.trim().toLowerCase();
		StringBuffer sb = new StringBuffer();

		if(!limitSupport){
			sb.append("select * from (select xxx.*, rownum as rrr from (");
			sb.append(sql);
			sb.append(") xxx) ttt where ttt.rrr > ");
			sb.append(offset);
			sb.append(" and ttt.rrr <= ");
			sb.append(offset + limit);
		}else{
			// mysql style
			sb.append("select ttt.* from (");
			sb.append(sql);
			sb.append(") ttt limit ");
			sb.append(offset);
			sb.append(",");
			sb.append(limit);
		}

		return sb.toString();
	}

	public static String getCountSql(String sql){
		sql	= sql.trim().toLowerCase();
		StringBuffer sb = new StringBuffer();

		sb.append("select count(*) as ");
		sb.append(RS_COLUMN);
		sb.append("	from (");
		sb.append(sql);
		sb.append(") ttt");

		return sb.toString();
	}
}